--离线副本玩法

--#include "data\config\welfare\OfflineExpCopy.lua" once


--[[	--数据结构  ActorStaticVar.offlineExpCopy
	state --地图状态 0 未开启 1 开启
	hours --离线时间 按照小时计算
	conds = {} 		开放条件， 或条件    每个或条件一个byte      byte的每位一个与条件 
]]

--[[	--数据结构  ActorStaticVar.offlineExpCopyMonster
	[monsterid] = num 杀怪数量
]]


function IsOfflineExpOpen(sysarg)
	return Actor.checkActorLevel(sysarg, OfflineExpCopyCfg.OpenLevel[2], OfflineExpCopyCfg.OpenLevel[1])
end

--协议下发
function SendOfflineExpCopy(sysarg)
	if not IsOfflineExpOpen(sysarg) then return end
    local npack = DataPack.allocPacket(sysarg,LogicCmd.trSys.sysId,LogicCmd.trSys.sub.sGetOfflineExpCopyData)  --申请一个数据包
    if (npack == nil) then
        --print("npack:nil")
        return
    end
   
   	local var = Actor.getStaticVar(sysarg)
   	if not var.offlineExpCopy then
   		var.offlineExpCopy = {}
   	end
   	local len = #OfflineExpCopyCfg.OfflineExpScenes
   	local offlineHour = 0
   	--print("len = "..len)
   	DataPack.writeByte(npack,len)
   	local awards = {}
   	for idx = 1, len do
   		local cfg = OfflineExpCopyCfg.OfflineExpScenes[idx]
   		if not var.offlineExpCopy[idx] then
   			var.offlineExpCopy[idx] = {}
   		end
   		local state = var.offlineExpCopy[idx].state or 0
   		--print("state = "..state)
   		DataPack.writeByte(npack,state)
   		if state == 0 then
   			local condLen = #cfg.GetCond
   			--print("condLen = "..condLen) 
   			DataPack.writeByte(npack,condLen)
   			if not var.offlineExpCopy[idx].conds then
   				var.offlineExpCopy[idx].conds = {}
   			end
   			for ii = 1, condLen do
   				local andCondLen = #cfg.GetCond[ii]
   				--print("andCondLen = "..andCondLen) 
   				DataPack.writeByte(npack, andCondLen)
   				local orCondState = var.offlineExpCopy[idx].conds[ii] or 0
   				for jj = 1, andCondLen do
   					local andCondState = System.getIntBit(orCondState, jj-1)
   					--print("andCondState = "..andCondState) 
   					DataPack.writeByte(npack, andCondState)
   				end
   			end
   		end
   		local copyHours = var.offlineExpCopy[idx].hours or 0
   		if  copyHours > offlineHour then
   			--print("hours = "..copyHours) 
   			offlineHour = copyHours
   		end
   		if state > 0 and copyHours > 0 then
   			--PrintAwardsTable(awards)
   			awards = MergeAward(awards,GetAwardsByRate(cfg.EveryHourAwards, copyHours))
   			--PrintAwardsTable(awards)
   		end
   	end

   	local getState = 0
   	if offlineHour > 0 then
   		getState = 1
   	end

   	--print("getState = "..getState)
   	--print("offlineHour = "..offlineHour)

   	DataPack.writeByte(npack,getState)  
   	DataPack.writeShort(npack,offlineHour)  

   	local awardLen = #awards

   	DataPack.writeByte(npack,awardLen) 

   	for idx = 1, awardLen do
   		DataPack.writeByte(npack,awards[idx].type)
   		DataPack.writeUInt(npack,awards[idx].count)
   	end

    DataPack.flush(npack)   --发送数据包
end

--事件出发副本开启
function ActiveOfflineExpCopy(sysarg, event, param, param2)
	--print("event = "..event)
   	--print("param ="..param)
   	--print("param2 ="..param2)
	local var = Actor.getStaticVar(sysarg)
	if not var.offlineExpCopyMonster then
		var.offlineExpCopyMonster = {}
	end
	if event ==OfflineExpSceneGetEvent.offlineKillMonster then
		var.offlineExpCopyMonster[param] = (var.offlineExpCopyMonster[param] or 0) + param2
	end

	if not var.offlineExpCopy then
   		var.offlineExpCopy = {}
   	end
   	local len = #OfflineExpCopyCfg.OfflineExpScenes
   	for idx = 1, len do
   		local cfg = OfflineExpCopyCfg.OfflineExpScenes[idx]
   		if not var.offlineExpCopy[idx] then
   			var.offlineExpCopy[idx] = {}
   		end
   		local state = var.offlineExpCopy[idx].state or 0
   		if state == 0 then
   			local condLen = #cfg.GetCond
   			if not var.offlineExpCopy[idx].conds then
   				var.offlineExpCopy[idx].conds = {}
   			end
   			for ii = 1, condLen do
   				local orCondState = var.offlineExpCopy[idx].conds[ii] or 0
   				local orCondCfg = cfg.GetCond[ii]
   				local stateFlag = true
   				local andCondlen = #orCondCfg
   				for jj = 1, andCondlen do
   					local andCondCfg = orCondCfg[jj]
   					local byteState = System.getIntBit(orCondState, jj-1)
   					if byteState == 1 then
   					elseif andCondCfg.event == event then
   						--print("event = "..event)
   						--print("param ="..param)
   						if event == OfflineExpSceneGetEvent.offlineVipLevel and param >= andCondCfg.param then
   							orCondState = System.setIntBit(orCondState, jj-1, true)
   						elseif event == OfflineExpSceneGetEvent.offlineKillMonster and var.offlineExpCopyMonster[andCondCfg.param] and var.offlineExpCopyMonster[andCondCfg.param] >= andCondCfg.param2 then
   							orCondState = System.setIntBit(orCondState, jj-1, true)
   						elseif event == OfflineExpSceneGetEvent.offlineExpActorLevel and Actor.checkActorLevel(sysarg, andCondCfg.param2, andCondCfg.param) then
   							orCondState = System.setIntBit(orCondState, jj-1, true)
   						else
   							stateFlag = false
   						end
   					else
   						stateFlag = false
   					end
   				end
   				var.offlineExpCopy[idx].conds[ii] = orCondState
   				if stateFlag then
   					var.offlineExpCopy[idx].state = 1
   					break
   				end
   			end
   		end
   	end
end

--领取离线奖励
function OfflineExpGetAward(sysarg, factor)
	if not IsOfflineExpOpen(sysarg) then return end
	local var = Actor.getStaticVar(sysarg)
   	if not var.offlineExpCopy then
   		var.offlineExpCopy = {}
   	end
   	local len = #OfflineExpCopyCfg.OfflineExpScenes
   	local awards = {}
   	for idx = 1, len do
   		local cfg = OfflineExpCopyCfg.OfflineExpScenes[idx]
   		if not var.offlineExpCopy[idx] then
   			var.offlineExpCopy[idx] = {}
   		end
   		local state = var.offlineExpCopy[idx].state or 0
   		--print("state = "..state)
   		local copyHours = var.offlineExpCopy[idx].hours or 0
   		if state > 0 and copyHours > 0 then
   			--PrintAwardsTable(awards)
   			awards = MergeAward(awards,GetAwardsByRate(cfg.EveryHourAwards, copyHours))
   			--PrintAwardsTable(awards)
   		end
   	end



	if not awards then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OfflineExpCopy001, ttFlyTip)
		return
	end
	local value = 0
	for idx, award in ipairs(awards) do
		if award.type == qatExp then
			value = award.count
		end
	end
	if factor == 2 or factor == 3 then
		local awardConsume = OfflineExpCopyCfg.AwardCondition[factor-1]
		--[[
		local awardConsume = GetElemByCond( awardConsumes, value )
		if not awardConsume then
			awardConsume = awardConsumes[#awardConsumes]
		end
		]]

		local viplevel = Actor.getVipGrade(sysarg)
		if viplevel < awardConsume.needVipLevel then
			Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.OfflineExpCopy002, awardConsume.needVipLevel), ttFlyTip)
			return
		end

		local count = math.floor(value/awardConsume.preNum)
      if count < 1 then
         count = 1
      end
		local cost = count * awardConsume.CostMoneyNum

		if Actor.getMoneyCount(sysarg, awardConsume.costMoneyType) < cost then
			--local moneyName = moneyTypeName[awardConsume.costMoneyType]
			--Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.OfflineExpCopy003, moneyName, cost), ttFlyTip)
         Actor.sendItemLack( sysarg, qatYuanbao, 0, cost )
			return
		end

		if not Actor.delMoney( sysarg,awardConsume.costMoneyType,cost,LogIds.OfflineExpFactorConsumeLogId,Lang.LogTips.OfflineExpCosume) then
			return
		end

		awards = GetAwardsByRate(awards, factor)
	end
  

  	--重置时间
   	if not var.offlineExpCopy then
   		var.offlineExpCopy = {}
   	end
   	local len = #OfflineExpCopyCfg.OfflineExpScenes
   	for idx = 1, len do
   		if not var.offlineExpCopy[idx] then
   			var.offlineExpCopy[idx] = {}
   		end
   		local state = var.offlineExpCopy[idx].state or 0
   		if state > 0 then
   			var.offlineExpCopy[idx].hours = 0
   		end   		
   	end

	GiveCommonAward(sysarg, awards, LogIds.OfflineExpAwardLogId, Lang.LogTips.OfflineExpAward)

	SendOfflineExpCopy(sysarg)
end

--玩家杀死对应怪物
function OfflineExpMonsterDie(monster,sysarg,monId)
	ActiveOfflineExpCopy(sysarg, OfflineExpSceneGetEvent.offlineKillMonster, monId, 1)
end

--玩家角色等级改变
function OfflineExpActorLevelChange(sysarg)
	local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	local cirlevel = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
	ActiveOfflineExpCopy(sysarg, OfflineExpSceneGetEvent.offlineExpActorLevel, cirlevel, level)
end

--玩家vip等级改变
function OfflineExpActorVipLevelChange(sysarg)
	local viplevel = Actor.getVipGrade(sysarg)
	ActiveOfflineExpCopy(sysarg, OfflineExpSceneGetEvent.offlineVipLevel, viplevel, 0)
end

--玩家下线
function OfflineExpLogoutHandler(sysarg)
   if not IsOfflineExpOpen(sysarg) then return end
   local var = Actor.getStaticVar(sysarg)
   var.offlineExpCopyTime = System.getCurrMiniTime()
   --print("var.offlineExpCopyTime ="..var.offlineExpCopyTime)
end

--玩家上线
function OfflineExpLoginHandler(sysarg)
   if not IsOfflineExpOpen(sysarg) then return end
   local var = Actor.getStaticVar(sysarg)
   local now = System.getCurrMiniTime()
   if var.offlineExpCopyTime then
      local value = now - var.offlineExpCopyTime
      --print("value ="..value)
      value = math.floor(value / 3600)
      if value > 0 then
         if not var.offlineExpCopy then
            var.offlineExpCopy = {}
         end
         local len = #OfflineExpCopyCfg.OfflineExpScenes
         for idx = 1, len do
            local cfg = OfflineExpCopyCfg.OfflineExpScenes[idx]
            if not var.offlineExpCopy[idx] then
               var.offlineExpCopy[idx] = {}
            end
            local state = var.offlineExpCopy[idx].state or 0
            if state > 0 then
               var.offlineExpCopy[idx].hours = (var.offlineExpCopy[idx].hours or 0) + value
               if var.offlineExpCopy[idx].hours > cfg.MaxHour then
                  var.offlineExpCopy[idx].hours = cfg.MaxHour
               end
            end
         end
      end
      var.offlineExpCopyTime = nil
   end
end


--离线副本协议处理
OfflineExpCopyMessageHandle = function (sysarg, systemid, msgid, packet)
   if msgid == LogicCmd.trSys.sub.cGetOfflineExpCopyData then     
      SendOfflineExpCopy(sysarg)
   elseif msgid == LogicCmd.trSys.sub.cGetOfflineExpCopyAward then 
      local factor = DataPack.readByte(packet) 
      OfflineExpGetAward(sysarg, factor)
   end
end

